package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.Clazz;
import com.itheima.entity.PageBean;
import com.itheima.entity.Student;
import com.itheima.entity.StudentQueryParam;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询
     * @param param
     * @return
     */
    @Override
    public PageBean page(StudentQueryParam param) {
        //1.设置分页参数
        PageHelper.startPage(param.getPage(),param.getPageSize());

        //2.调用mapper
        List<Student> studentList = studentMapper.list(param);
        Page p = (Page)studentList;

        //3.封装PageBean对象，并返回
        return new PageBean(p.getTotal(),p.getResult());
    }

    /**
     * 添加学生
     * @param student
     */
    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.add(student);
    }

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    @Override
    public Student getById(Integer id) {
        Student student = studentMapper.getById(id);
        return student;
    }

    /**
     * 修改学生
     * @param student
     */
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    /**
     * 批量删除学生
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    /**
     * 添加违纪
     * @param id
     * @param score
     */
    @Override
    public void missScore(Integer id, Short score) {
        studentMapper.missScore(id,score);
    }
}
